package main

//@title bluebell_backend
//@version 1.0
//@description news_app 测试
//@termsOfService http://swagger.io/terms/
//
//@contact.name author：@lxj
//@contact.url http://www.swagger.io/support
//@contact.email support@swagger.io
//
//@license.name Apache 2.0
//@license.url http://www.apache.org/licenses/LICENSE-2.0.html
//
//@host 127.0.0.1:8081
//@BasePath /api/v1/

import (
	"fmt"
	"news/dao/mysql"
	"news/dao/redis"
	"news/logger"
	"news/pkg/snowflake"
	"news/routes"
	"news/settings"

	"go.uber.org/zap"
)

func main() {
	if err := settings.Init(); err != nil {
		fmt.Printf("init settings failed err %s\n", err)
		return
	}
	if err := logger.Init(settings.Conf.LogConfig, settings.Conf.Mode); err != nil {
		fmt.Printf("init logger failed err %s\n", err)
		return
	}
	defer zap.L().Sync()
	zap.L().Debug("logger init success")

	if err := mysql.Init(settings.Conf.MySQLConfig); err != nil {
		fmt.Printf("init mysql failed err %s\n", err)
		return
	}
	defer mysql.Close()

	if err := redis.Init(); err != nil {
		fmt.Printf("init redis failed err %s\n", err)
		return
	}
	defer redis.Close()

	if err := snowflake.Init(settings.Conf.StartTime, settings.Conf.MachineID); err != nil {
		fmt.Printf("init snowflake failed, err:%v\n", err)
		return
	}

	// 注册路由
	r := routes.Setup(settings.Conf.Mode)
	err := r.Run(fmt.Sprintf(":%d", settings.Conf.Port))
	if err != nil {
		fmt.Printf("run server failed, err:%v\n", err)
		return
	}

}
